/*
 * @Author: jingfuu@163.com
 * @Date: 2024-11-13 14:07:34
 * @LastEditTime: 2024-12-11 13:36:28
 * @Description: file content
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Inject,
  Logger,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import MenuService from '../service/MenuService';
import { Constants } from 'src/common/Constants';
import { R } from 'src/common/R';
import { MenuDto } from '../dto/MenuDto';
import MenuQueryParam from '../param/MenuQueryParam';
import PageResult from 'src/common/PageResult';
import { PreAuthorize } from 'src/common/decorator/pre-authorize.decorator';
import { Log } from 'src/common/decorator/log';
import BusinessType from 'src/common/enum/BusinessType';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-11-13 14:07:34
 * @LastEditTime: 2024-11-26 09:14:26
 * @Description: 菜单接口
 */
@ApiTags('菜单')
@Controller('system/menu')
class MenuController {
  private readonly logger = new Logger(MenuController.name);

  @Inject()
  private menuService: MenuService;

  @ApiOperation({ summary: '获取角色菜单树' })
  @Get('roleMenuTreeselect/:roleId(\\d+)')
  async getRoleMenuTree(@Param('roleId') roleId: number) {
    return {
      checkedKeys: await this.menuService.getCheckedMenuId(roleId),
      menus: await this.menuService.getAllMenuTree(),
      code: Constants.SUCCESS,
    };
  }

  @PreAuthorize('system:menu:query')
  @ApiOperation({ summary: '获取菜单详情' })
  @Get('/:id(\\d+)')
  async getMenuDetail(@Param('id') id: number) {
    return R.ok(await this.menuService.getDetailById(id));
  }

  @ApiOperation({ summary: '获取菜单树形数据' })
  @Get('treeselect')
  async getMenuTree() {
    return R.ok(await this.menuService.getAllMenuTree());
  }

  @PreAuthorize('system:menu:list')
  @ApiOperation({ summary: '查询菜单列表' })
  @Get('/list')
  async findMenuList(@Query() param: MenuQueryParam): Promise<R<MenuDto[]>> {
    param.pageSize = 1000;
    this.logger.log('query menu list start, param:' + JSON.stringify(param));
    const result: PageResult<MenuDto> = await this.menuService.list(param);
    return R.ok(result.rows);
  }

  @Log({ title: '菜单管理', businessType: BusinessType.DELETE })
  @PreAuthorize('system:menu:remove')
  @ApiOperation({ summary: '删除菜单' })
  @Delete('/:id')
  async deleteMenu(@Param('id') id: string): Promise<R<any>> {
    if (id.indexOf(',') > -1) {
      for (const item of id.split(',')) {
        await this.menuService.deleteMenu(Number.parseInt(item));
      }
    } else {
      await this.menuService.deleteMenu(Number.parseInt(id));
    }
    return R.ok();
  }

  @Log({ title: '菜单管理', businessType: BusinessType.UPDATE })
  @PreAuthorize('system:menu:edit')
  @ApiOperation({ summary: '修改菜单' })
  @Put()
  async modifyMenu(@Body() menu: MenuDto): Promise<R<any>> {
    await this.menuService.updateMenu(menu);
    return R.ok();
  }

  @Log({ title: '菜单管理', businessType: BusinessType.INSERT })
  @PreAuthorize('system:menu:add')
  @ApiOperation({ summary: '新增菜单' })
  @Post()
  async createMenu(@Body() menu: MenuDto): Promise<R<any>> {
    await this.menuService.createMenu(menu);
    return R.ok();
  }
}
export default MenuController;
